package com.ruoyi.aitcommon.api;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aitapp.utils.APPRuntimeManager;
import com.ruoyi.aitapp.vo.LicenseInfoVO;
import com.ruoyi.aitcommon.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Base64;

@RestController
@RequestMapping("/ait/common/lincense")
public class LicenseOperation {
    @Autowired
    APPRuntimeManager appRuntimeManager;
    @Autowired
    LicenseUtils licenseUtils;

    /**
     * 下载许可申请文件
     * 返回文件流时,header中添加CONTENT_DISPOSITION,contentType=APPLICATION_OCTET_STREAM,浏览器会自动下载
     * @return 文件流
     */
    @GetMapping("/downloadRequest")
    public ResponseEntity<InputStreamResource> downloadRequest() throws Exception {
        File file = licenseUtils.getRequestFile();
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamResource resource = new InputStreamResource(fileInputStream);

        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + file.getName() + "\"")
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .contentLength(file.length())
                .body(resource);
    }

    /**
     * 导入许可文件
     * 使用postman时,需要设置body的type为form-data,并添加file参数
     * @param file 前端控件上传的文件流
     * @return
     */
    @PostMapping("/importPermit")
    public JSONObject importPermit(MultipartFile file)
    {
        try{
            if(file==null) throw new Exception("传入文件为空");
            final InputStream inputStream = file.getInputStream();
            licenseUtils.writePermitFile(inputStream);
            return JSONResult.success("许可文件导入成功");
        }catch (Exception ex){
            return JSONResult.error(ex.getMessage());
        }
    }

    /**
     * 将许可申请传入,生成许可文件
     * 使用postman时,需要设置body的type为form-data,并添加file参数
     * @param file 前端控件上传的许可申请
     * @return 许可文件路径
     */
    @PostMapping("/createPermit")
    public JSONObject createPermit(MultipartFile file,int maxUserNum,String startDate,String endDate,String path)
    {
        try{
            if(file==null) throw new Exception("传入文件为空");
            final InputStream inputStream = file.getInputStream();
            final String content = FileUtils.readFromStream(inputStream);
            final File permitFile = licenseUtils.createPermitFile(content,maxUserNum,startDate,endDate,path);
            JSONObject ret=new JSONObject();
            ret.put("filePath",permitFile.getAbsolutePath());
            return JSONResult.success("许可文件导入成功",ret);
        }catch (Exception ex){
            return JSONResult.error(ex.getMessage());
        }
    }

    /**
     * 获取当前许可信息
     * @return
     */
    @GetMapping("/getPermitInfo")
    public JSONObject getPermitInfo()
    {
        try{
            final LicenseInfoVO licenseInfo = licenseUtils.getLicenseInfo();
            return JSONResult.success(licenseInfo);
        }catch (Exception ex){
            return JSONResult.error(ex.getMessage());
        }
    }
}
